<?php
class Boards extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$category = new Category($_GET['agency_id']);
		$data['contents'] = $category->child->get_by_related_structure('type', 'board')->board;
		if(!empty($_GET['group_id']))$data['contents']->where('group_id',$_GET['group_id']);
		$data['contents']->get_page(50);
		$this->template->build('admin/index', $data);
	}
	
	public function form($id = NULL)
	{
		$category = new Category($_GET['agency_id']);
		$data['category'] = $category->child->get_by_related_structure('type', 'board');
		$data['content'] = new Board($id);
		$this->template->append_metadata(js_datepicker());
		$this->template->build('admin/form', $data);
	}
	
	public function save($id = NULL)
	{
		if($_POST)
		{
			$content = new Board($id);
			if(@$_FILES['image']['name'])
			{
				if($id) $content->delete_file('uploads/board');
				$content->image = $content->upload($_FILES['image'], 'uploads/board', 100, 125);
			}
			if(isset($_POST['budget_year_start']))$_POST['budget_year_start'] = Date2DB($_POST['budget_year_start']);
			if(isset($_POST['budget_year_end']))$_POST['budget_year_end'] = Date2DB($_POST['budget_year_end']);
			$content->from_array($_POST);
			$content->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect('boards/admin/boards/?agency_id='.$_GET['agency_id']);
	}
	
	public function delete($id)
	{
		if($id)
		{
			$content = new Board($id);
			$content->delete_file('uploads/board');
			$content->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect('boards/admin/boards/?agency_id='.$_GET['agency_id']);
	}
	
	public function delete_img($id)
	{
		if($id)
		{
			$content = new Board($id);
			$content->delete_file('uploads/board');
			$content->image = '';
			$content->save();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
